package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.ModelArtList;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.vo.ArticleLikeInfoVO;
import com.example.demo.service.ArticleSevice;
import com.example.demo.service.LikeRcountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-09
 * Time: 21:18
 */
@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleSevice articleSevice;

    @Autowired
    private LikeRcountService likeRcountService;

    @RequestMapping("/artlist")
    public AjaxResult getArtList(HttpServletRequest request) {
        // 先从session中获取到用户信息
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求!");
        }
        List<ArticleInfo> artlist = articleSevice.getArtListByUid(userInfo.getId());
        ModelArtList.modelArtList(artlist);
        return AjaxResult.success(artlist);
    }

    @RequestMapping("/artdel")
    public AjaxResult delArtById(HttpServletRequest request, Integer id) {
        // 先获取到用户信息
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求!");
        }
        Integer result = articleSevice.delArtById(id, userInfo.getId());
        return AjaxResult.success(result);
    }

    @RequestMapping("/detail")
    public AjaxResult getArtDetail(Integer id) {
        // 判断文章id的有效性
        if (id == null && id <= 0) {
            return AjaxResult.fail(-1, "非法参数!");
        }
        ArticleInfo articleInfo = articleSevice.getArtDetail(id);
        return AjaxResult.success(articleInfo);
    }

    @RequestMapping("/incre-rcount")
    public AjaxResult increRCount(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数!");
        }
        return AjaxResult.success(articleSevice.increRCount(id));
    }

    @RequestMapping("/addart")
    public AjaxResult addArt(HttpServletRequest request, ArticleInfo articleInfo) {
        // 参数的非空校验
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent())) {
            // 此时前端传递的数据有问题
            return AjaxResult.fail(-1, "非法参数!");
        }
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null || userInfo.getId() == null) {
            return AjaxResult.fail(-2, "非法的用户请求!");
        }
        articleInfo.setUid(userInfo.getId());
        return AjaxResult.success(articleSevice.addArt(articleInfo));
    }

    @RequestMapping("/update")
    public AjaxResult updateArt(HttpServletRequest request, ArticleInfo articleInfo) {
        // 非空校验
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent()) || articleInfo.getId() == null) {
            // 此时参数有问题
            return AjaxResult.fail(-1, "非法参数!");
        }
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null || userInfo.getId() == null) {
            return AjaxResult.fail(-2, "非法的用户请求!");
        }
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleSevice.updateArt(articleInfo));
    }

    /**
     * @param pindex 当前页数
     * @param psize  当前页的显示个数
     */
    @RequestMapping("/getartbypage")
    public AjaxResult getArtByPage(Integer pindex, Integer psize) {
        // 进行对参数校正
        if (pindex == null || pindex <= 1) {
            pindex = 1;
        }
        if (psize == null || psize <= 1) {
            psize = 4;
        }
        // 通过页数 + 页显示个数 计算出偏移量
        int offset = (pindex - 1) * psize;
        List<ArticleInfo> list = articleSevice.getArtByPage(psize, offset);
        ModelArtList.modelArtList(list);

        // 先获取到文章的总数
        int totalCount = articleSevice.getArtTotalCount();
        double totalPageDB = totalCount / (psize * 1.0);// 此时获得是总的页数的double值，还需要向上取整
        int totalPage = (int) Math.ceil(totalPageDB);
        // 通过HasMap传递两个值
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("list", list);
        hashMap.put("pcount", totalPage);
        return AjaxResult.success(hashMap);
    }

    @RequestMapping("/liketreadcount")
    public AjaxResult getLikeTreadCount(HttpServletRequest request, Integer aid) {
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(0, "当前用户未登录!");
        }
        int uid = userInfo.getId();
        // 先通过文章的id进行判断有没有进行过管理
        int result = likeRcountService.getArtUserEdit(aid, uid);
        if (result == 0) {
            // 说明该篇文章没有进行过赞或者踩
            return AjaxResult.success(200, null);
        }
        int likestate = likeRcountService.getArtLikeState(aid, uid);
        int treadstate = likeRcountService.getArtTreadState(aid, uid);
        ArticleInfo articleInfo = articleSevice.getArtDetail(aid);
        ArticleLikeInfoVO articleLikeInfoVO = new ArticleLikeInfoVO();
        articleLikeInfoVO.setLikecount(articleInfo.getLikecount());
        articleLikeInfoVO.setTreadcount(articleInfo.getTreadcount());
        articleLikeInfoVO.setLikestate(likestate);
        articleLikeInfoVO.setTreadstate(treadstate);
        return AjaxResult.success(articleLikeInfoVO);
    }

    @RequestMapping("/editlikecount")
    public AjaxResult getAddArtlikeCount(HttpServletRequest request, Integer aid) {
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null) {
            // 当前用户未登录
            return AjaxResult.fail(-1, "该用户未登录！");
        }
        // 先去获取一下，该用户是否已经对该文章点赞获取踩进行操作过
        int result = likeRcountService.getArtUserEdit(aid, userInfo.getId());
        if (result == 0) {
            // 说明该用户未点赞过，此时需要插入该用户对文章的操作记录，并把点赞状态改为1
            likeRcountService.addArtUserEdit(aid, userInfo.getId());
            articleSevice.addArtLikeCount(aid);
            likeRcountService.updateArtUserLike(aid, userInfo.getId());
            ArticleInfo articleInfo = articleSevice.getArtDetail(aid);
            articleInfo.setContent("");
            return AjaxResult.success(articleInfo);
        }
        // 此时根据点赞状态和踩的状态进行划分情况
        if (likeRcountService.getArtLikeState(aid, userInfo.getId()) == 1 && likeRcountService.getArtTreadState(aid, userInfo.getId()) == 0) {
            // 此时已经点赞过了，需要取消点赞  将点赞状态修改为 0 并将点赞数量 --
            likeRcountService.delArtUserLike(aid, userInfo.getId());
            articleSevice.delArtLikeCount(aid);
            ArticleInfo articleInfo = articleSevice.getArtDetail(aid);
            articleInfo.setContent("");
            return AjaxResult.success(0, articleInfo);
        } else if (likeRcountService.getArtLikeState(aid, userInfo.getId()) == 0 && likeRcountService.getArtTreadState(aid, userInfo.getId()) == 0) {
            // 此时说明之前，操作过该篇文章，并重新进行点赞操作 - 》 点赞数量 ++ 状态 ： 0 -》 1
            likeRcountService.updateArtUserLike(aid, userInfo.getId());
            articleSevice.addArtLikeCount(aid);
            ArticleInfo articleInfo = articleSevice.getArtDetail(aid);
            articleInfo.setContent("");
            return AjaxResult.success(articleInfo);
        } else if (likeRcountService.getArtLikeState(aid, userInfo.getId()) == 0 && likeRcountService.getArtTreadState(aid, userInfo.getId()) == 1) {
            // 此时代表踩已经点了，现在要点击点赞功能，不允许在踩之后，再对文章进行点赞
            return AjaxResult.fail(-2, "该文章已经踩过了，请先取消踩，再进行点赞");
        } else {
            return AjaxResult.fail(-3, "点赞失败!");
        }
    }

    @RequestMapping("/edittreadcount")
    public AjaxResult getAddArtTreadCount(HttpServletRequest request, Integer aid) {
        // 判断用户是否是登录状态
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if (userInfo == null) {
            // 当前用户未登录
            return AjaxResult.fail(-1, "该用户未登录！");
        }
        // 先去获取一下，该用户是否已经对该文章点赞获取踩进行操作过
        int result = likeRcountService.getArtUserEdit(aid, userInfo.getId());
        if (result == 0) {
            // 说明该用户未踩过，此时需要插入该用户对文章的操作记录，并把踩状态改为1
            likeRcountService.addArtUserEdit(aid, userInfo.getId());
            articleSevice.addArtTreadCount(aid);
            likeRcountService.updateArtUserTread(aid, userInfo.getId());
            ArticleInfo articleInfo = articleSevice.getArtDetail(aid);
            articleInfo.setContent("");
            return AjaxResult.success(articleInfo);
        }
        // 此时根据点赞状态和踩的状态进行划分情况
        if (likeRcountService.getArtTreadState(aid, userInfo.getId()) == 1 && likeRcountService.getArtLikeState(aid, userInfo.getId()) == 0) {
            // 此时已经踩过了，需要取消踩 将踩状态修改为 0 并将踩数量 --
            likeRcountService.delArtUserTread(aid, userInfo.getId());
            articleSevice.delArtTreadCount(aid);
            ArticleInfo articleInfo = articleSevice.getArtDetail(aid);
            articleInfo.setContent("");
            return AjaxResult.success(0, articleInfo);
        } else if (likeRcountService.getArtTreadState(aid, userInfo.getId()) == 0 && likeRcountService.getArtLikeState(aid, userInfo.getId()) == 0) {
            // 此时说明之前，操作踩过该篇文章，并重新进行踩操作 - 》 踩数量 ++ 状态 ： 0 -》 1
            likeRcountService.updateArtUserTread(aid, userInfo.getId());
            articleSevice.addArtTreadCount(aid);
            ArticleInfo articleInfo = articleSevice.getArtDetail(aid);
            articleInfo.setContent("");
            return AjaxResult.success(articleInfo);
        } else if (likeRcountService.getArtTreadState(aid, userInfo.getId()) == 0 && likeRcountService.getArtLikeState(aid, userInfo.getId()) == 1) {
            // 此时代表点赞已经点了，现在要点击踩功能，不允许在点赞之后，再对文章进行踩
            return AjaxResult.fail(-2, "该文章已经点赞过了，请先取消点赞，再进行踩");
        } else {
            return AjaxResult.fail(-3, "踩失败!");
        }
    }
}
